package edu.lingnan.house.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.lingnan.house.pojo.TbAgent;
import edu.lingnan.house.pojo.TbBooking;
import edu.lingnan.house.pojo.TbHouse;
import edu.lingnan.house.pojo.TbUser;
import edu.lingnan.house.service.TbAgentService;
import edu.lingnan.house.service.TbBookingService;
import edu.lingnan.house.service.TbHouseService;
import edu.lingnan.house.service.TbUserService;
import edu.lingnan.house.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("lin-book")
@CrossOrigin
public class BookController {
    @Autowired
    private TbBookingService tbBookingService;

    @Autowired
    private TbHouseService tbHouseService;

    @Autowired
    private TbAgentService tbAgentService;

    @Autowired
    private TbUserService tbUserService;



    @PostMapping("/saveBook")
    public Result Book(HttpSession httpSession, String bookTime, Integer houseId){
        //把bookTime从String类型转为LocalDateTime类型
        LocalDateTime localDateTime = LocalDateTime.parse(bookTime);

         //获取localDateTime的日，方便后面作比较
        Integer bookDay = localDateTime.getDayOfYear();

        //获取登陆用户信息
        TbUser tbUser = (TbUser) httpSession.getAttribute("tbUser");
        //判断是否登录
        if(tbUser==null){
            return new Result(203,"请先登录");
        }

        String id = tbUser.getId();

        //判断预约的时间是否在现在之后
        Integer now = LocalDateTime.now().getDayOfYear();
        Integer n =bookDay - now;
        if(n < 1){
            return new Result(201,"请至少提前一天预约");
        }

        //判断是否已经预约了
        QueryWrapper<TbBooking> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",id).eq("house_id",houseId);
        TbBooking tbBooking1 = tbBookingService.getOne(queryWrapper1);
        if(tbBooking1 != null){
            if(tbBooking1.getState() != 3){
                return new Result(202,"你提交的预约信息正在处理中，请勿重新预约");
            }
        }


        //通过houseId查询对应中介信息
        TbHouse tbHouse = tbHouseService.getById(houseId);
        String agentId = tbHouse.getAgentId();
        QueryWrapper<TbAgent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",agentId);
        TbAgent tbAgent = tbAgentService.getOne(queryWrapper);

        //插入预约信息
        TbBooking tbBooking = new TbBooking().setUserName("134ll").setUserId("134")
                .setAgentName(tbAgent.getName()).setAgentId(tbAgent.getId()).setAddress(tbHouse.getAddress()).setTime(localDateTime)
                .setState(1).setHouseId(houseId).setCreateTime(LocalDateTime.now());
        Boolean insert = tbBookingService.save(tbBooking);
        return new Result(200,insert,"ok");
    }

}
